1 00:00:00,370 --> 00:00:01,090 Welcome. 2 00:00:01,090 --> 00:00:01,930 In this lecture. 3 00:00:01,930 --> 00:00:07,210 We're going to take a look at how to use module scripts and object oriented programming to create some 4 00:00:07,210 --> 00:00:11,440 house plots that you may or may not have seen in many popular games. 5 00:00:11,830 --> 00:00:17,710 I've attached this model to the lecture, and it's just a small house that has a mailbox and this black 6 00:00:17,710 --> 00:00:19,630 part with it and this black part. 7 00:00:19,660 --> 00:00:22,600 We're going to use to allow a player to claim the house. 8 00:00:22,600 --> 00:00:28,630 And then on the mailbox I have the surface GI that will display the owner's name for this house. 9 00:00:28,870 --> 00:00:34,120 And then there's also a click detector in this front door that the owner of the house can click to lock 10 00:00:34,120 --> 00:00:35,690 and unlock the front door. 11 00:00:35,710 --> 00:00:38,720 Now, before we get started, we need to do a little bit more setup. 12 00:00:38,740 --> 00:00:45,520 We're going to make a folder in the workspace and I'm just going to call it houses and we're going to 13 00:00:45,520 --> 00:00:49,480 set this small house to be a child of this folder. 14 00:00:49,870 --> 00:00:54,140 And then we're also going to need to create a folder in server storage. 15 00:00:54,160 --> 00:00:56,290 I'm going to call it classes. 16 00:00:57,210 --> 00:01:00,030 And then we're going to create a module script in here. 17 00:01:00,750 --> 00:01:07,200 In this module script is going to act as our blueprint or our class for a house object, so I'll just 18 00:01:07,200 --> 00:01:08,220 call it house. 19 00:01:08,220 --> 00:01:09,870 So this is the house class. 20 00:01:10,170 --> 00:01:16,530 Now inside of here, we can just delete this because we're going to need some services or actually we'll 21 00:01:16,530 --> 00:01:18,000 just need one service. 22 00:01:18,150 --> 00:01:20,580 We'll need some variables. 23 00:01:22,050 --> 00:01:24,090 We'll define a constant. 24 00:01:26,340 --> 00:01:31,680 And then this module script is going to contain some private functions or functions that will only be 25 00:01:31,680 --> 00:01:33,420 used within the module script. 26 00:01:33,420 --> 00:01:38,160 And then we're also going to have some public functions and these functions are going to be used when 27 00:01:38,160 --> 00:01:40,110 we create new house objects. 28 00:01:40,110 --> 00:01:45,000 And when we call these functions on these house objects, they'll use the functions that are in this 29 00:01:45,000 --> 00:01:46,110 module script. 30 00:01:46,940 --> 00:01:51,290 So first we need to get access to the player service. 31 00:01:52,190 --> 00:01:55,520 And we're going to be using this later, so don't worry about it for now. 32 00:01:55,730 --> 00:02:02,180 And then the first thing we need to do is create a table because module scripts have to return a table. 33 00:02:02,210 --> 00:02:09,710 So I'm going to call it house and I'm going to use a capital H because this is basically our class and 34 00:02:09,710 --> 00:02:11,420 it's just going to be an empty table. 35 00:02:11,690 --> 00:02:18,170 And then inside of this table, I'm going to set the underscore underscore index meta method to be equal 36 00:02:18,170 --> 00:02:20,540 to the house table. 37 00:02:20,750 --> 00:02:26,990 Now, if you remember in our module script and meta table videos, we set the index meta method to house. 38 00:02:26,990 --> 00:02:34,700 That way when we create a new house object, which is just a table, we're going to attach this house 39 00:02:34,700 --> 00:02:39,290 and this house is going to act as the meta table for our house table. 40 00:02:39,560 --> 00:02:44,930 And the reason we need to do that is because if we call any functions on that house, it's going to 41 00:02:44,930 --> 00:02:50,120 look well, hey, these functions don't exist within this table, but it'll see, hey, there's an index 42 00:02:50,120 --> 00:02:53,250 meta method and it has this house table attached to it. 43 00:02:53,250 --> 00:02:58,110 So then we'll look in this house table and see if there's any functions in there that match whatever 44 00:02:58,380 --> 00:02:59,280 we called on it. 45 00:03:00,680 --> 00:03:06,680 Next thing we're going to do is we're going to declare some functions in here that this class will need. 46 00:03:06,710 --> 00:03:10,880 One is going to be a function for checking whether or not a player owns a house. 47 00:03:10,880 --> 00:03:16,010 So we'll just call it player owns home and we'll pass a player to this function. 48 00:03:16,850 --> 00:03:23,420 We're going to need a function for when the front door gets clicked, so I'll call it on front door 49 00:03:23,420 --> 00:03:24,260 clicked. 50 00:03:25,180 --> 00:03:27,700 And for this function, we're going to have two parameters. 51 00:03:27,700 --> 00:03:33,550 One is going to be called self and then the other is going to be the player that clicked the door. 52 00:03:33,820 --> 00:03:39,880 Now this self is going to represent the house object that this click detector belongs to. 53 00:03:39,910 --> 00:03:45,310 So when the door gets clicked, we can listen and check to see if this player is the owner of the house 54 00:03:45,310 --> 00:03:48,340 by checking a parameter within this table. 55 00:03:48,340 --> 00:03:49,810 And we'll do that in a little bit. 56 00:03:51,400 --> 00:03:55,750 We'll also need a function for when that touch part in the house gets touched. 57 00:03:55,750 --> 00:03:59,740 So we'll call it on claim part touched. 58 00:03:59,860 --> 00:04:06,640 We'll also pass self and we also need to get the part that touched our claim part. 59 00:04:07,800 --> 00:04:13,140 We're just going to need one more function that belongs to the table, and that's going to be our constructor. 60 00:04:14,260 --> 00:04:16,890 So our function will declare one. 61 00:04:16,900 --> 00:04:21,580 So inside of our house table, we're going to declare a new function. 62 00:04:21,580 --> 00:04:24,130 This is going to be our constructor function. 63 00:04:24,990 --> 00:04:30,930 So whenever we need to create a new house object, we're going to call this new function that belongs 64 00:04:30,930 --> 00:04:36,060 to our table and it will return to us a table that represents our house object. 65 00:04:36,240 --> 00:04:41,700 And we're going to pass the model of our house, basically the small house model. 66 00:04:41,700 --> 00:04:44,820 And then the script will take care of everything else for us. 67 00:04:46,310 --> 00:04:51,350 So the first thing I want to do is I want to declare a variable in here and I'm just going to call it 68 00:04:51,350 --> 00:04:53,810 self, but you can name it whatever you'd like. 69 00:04:53,810 --> 00:04:59,120 I'm just going to call it self to stay consistent and it's going to be equal to set meta table. 70 00:04:59,150 --> 00:05:01,250 We're going to pass an empty table. 71 00:05:01,860 --> 00:05:05,880 And we're going to attach the house table to this empty table. 72 00:05:06,210 --> 00:05:09,000 So this this is our house object right here. 73 00:05:09,030 --> 00:05:09,660 This. 74 00:05:09,660 --> 00:05:10,590 This table. 75 00:05:11,500 --> 00:05:14,500 And attached to this table is this other table. 76 00:05:15,200 --> 00:05:19,190 So whenever we try to use any functions on this table. 77 00:05:19,190 --> 00:05:22,790 So, for example, we could do like a function like lock door. 78 00:05:22,820 --> 00:05:29,870 If we call this function on this table, it'll say, Hey, this table right here doesn't have this function 79 00:05:29,870 --> 00:05:30,320 in it. 80 00:05:30,320 --> 00:05:35,060 So let's go ahead and look inside of the meta table to see if it has this function. 81 00:05:35,390 --> 00:05:39,620 And since it will have that function, then it can do all the functionality there. 82 00:05:40,320 --> 00:05:43,360 So inside our house object, we're going to have a few properties. 83 00:05:43,380 --> 00:05:48,630 One, we need to know who the owner of the house is, but for now, we're going to set it to nil. 84 00:05:48,630 --> 00:05:49,830 So there's no owner. 85 00:05:50,710 --> 00:05:56,050 We want the model that belongs to this object, which will be the House model that is passed to this 86 00:05:56,050 --> 00:05:56,770 function. 87 00:05:58,250 --> 00:06:03,830 We need the click detector from this house model so we can do front door click. 88 00:06:03,860 --> 00:06:08,510 This will represent our click detector and we'll use the house model. 89 00:06:08,810 --> 00:06:12,380 And inside this model there's a front door model. 90 00:06:12,380 --> 00:06:15,050 And that front door model has a click detector. 91 00:06:17,220 --> 00:06:24,270 We also need to reference the claim part and again, how model and there's a part in there called claim 92 00:06:24,270 --> 00:06:24,870 part. 93 00:06:26,140 --> 00:06:30,400 And then we also want to access the guy that's on our mailbox. 94 00:06:30,400 --> 00:06:37,360 So I'll call it mailbox guy and it'll be equal to the house model dot mailbox. 95 00:06:37,630 --> 00:06:40,930 And then there's a part in there called label part. 96 00:06:40,930 --> 00:06:43,450 And that label part has a surface guy. 97 00:06:45,370 --> 00:06:52,330 And then we'll need two booleans to keep track of when our front door is locked and when the touch part 98 00:06:52,330 --> 00:06:53,320 has been touched. 99 00:06:53,320 --> 00:06:55,660 So we have a debounce for the touch part. 100 00:06:55,690 --> 00:07:02,350 That way this function can't get run multiple times, so we'll have one be called is front. 101 00:07:03,340 --> 00:07:08,950 Door locked, we'll set it to false and then we'll have one for the bounce. 102 00:07:08,950 --> 00:07:14,350 For our touch part, we'll just call it touched Bounce and we'll set that to false as well. 103 00:07:15,240 --> 00:07:20,430 And then what we need to do now is that when our claim part gets touched. 104 00:07:21,340 --> 00:07:23,860 We're going to connect a function to this. 105 00:07:23,860 --> 00:07:29,110 And inside this function we are going to call the on claim part touched function. 106 00:07:30,070 --> 00:07:36,430 And we need to get the other part that touched our clam part and we need to pass it to this function. 107 00:07:36,610 --> 00:07:39,550 But remember, we also need to pass the table. 108 00:07:39,580 --> 00:07:43,090 Our house object to the function, so we'll just pass self here too. 109 00:07:43,270 --> 00:07:48,190 Now, the reason that I've put this function in here, instead of just copying all the functionality 110 00:07:48,190 --> 00:07:56,050 and pasting it in here is because if I'm creating multiple house objects, that means I'm creating multiple 111 00:07:56,050 --> 00:07:59,500 anonymous functions that all contain the same code. 112 00:07:59,590 --> 00:08:06,550 So it'll take up more memory than if I just have it reference a function that I declared inside the 113 00:08:06,550 --> 00:08:07,210 module script. 114 00:08:07,210 --> 00:08:09,130 So I save on memory that way. 115 00:08:09,670 --> 00:08:13,630 Next, we need to listen to when the front door click detector gets clicked. 116 00:08:13,630 --> 00:08:17,950 So mouse click again, we'll connect a function. 117 00:08:19,120 --> 00:08:20,680 We'll get the player that clicked. 118 00:08:21,350 --> 00:08:23,990 And then we'll just call the on front door clicked. 119 00:08:24,020 --> 00:08:27,680 Pass the door object and the player that clicked the door. 120 00:08:27,890 --> 00:08:29,480 And then I'm pretty sure that's it. 121 00:08:29,510 --> 00:08:36,410 Once we do this, then we simply just want to return this table back to wherever this function was called. 122 00:08:36,770 --> 00:08:40,630 Now, another thing I want to do, actually, is I want to create another variable. 123 00:08:40,640 --> 00:08:42,380 I'm going to call it houses. 124 00:08:43,140 --> 00:08:44,610 It's going to be an empty table. 125 00:08:45,560 --> 00:08:50,450 And I'm going to insert in this table every new house object that gets created. 126 00:08:52,600 --> 00:08:57,550 And I want to do this to be able to check whether or not a player owns a house. 127 00:08:57,550 --> 00:09:02,920 So we need to check through every single house object whether or not a player is a owner of a home. 128 00:09:02,920 --> 00:09:09,070 And the only way to do that is to be able to store these tables inside of this table. 129 00:09:09,250 --> 00:09:16,060 So now that we're done with our constructor, for the most part, we need to create some public functions 130 00:09:16,060 --> 00:09:17,700 for the door class. 131 00:09:17,710 --> 00:09:19,180 So, for example. 132 00:09:20,040 --> 00:09:21,450 For our house object. 133 00:09:21,480 --> 00:09:24,300 We want to be able to set the owner. 134 00:09:25,600 --> 00:09:29,800 Of our house and this function will pass a player to it. 135 00:09:31,730 --> 00:09:39,530 I remember I'm using a colon here because when I create a new house object like self here, if I were 136 00:09:39,530 --> 00:09:47,090 to call this set owner function on it, this self, this table object automatically gets passed to the 137 00:09:47,090 --> 00:09:49,650 function as self. 138 00:09:49,670 --> 00:09:54,750 Self already exists inside of this function because I'm using this colon here. 139 00:09:54,770 --> 00:10:01,940 If I used a dot, I would have to pass self as a parameter in the function, but I just use a colon 140 00:10:02,150 --> 00:10:04,340 and self automatically gets passed. 141 00:10:04,820 --> 00:10:05,780 We'll delete that. 142 00:10:06,920 --> 00:10:12,920 So when we want to set the owner for the house, we'll just do self dot owner is equal to this player. 143 00:10:14,020 --> 00:10:20,710 And then what we could also do is we could access that mailbox guy that we created up here when we first 144 00:10:20,710 --> 00:10:22,360 created our house object. 145 00:10:23,160 --> 00:10:26,100 And inside of that guy, there's a text label. 146 00:10:26,890 --> 00:10:35,170 And we can set the text to something like the players name and then their home so we could do player.name 147 00:10:35,470 --> 00:10:43,210 and then concatenate it with Apostrophe S home so we could do something like Udemy, Lulu courses, 148 00:10:43,210 --> 00:10:44,500 home, whatever. 149 00:10:45,160 --> 00:10:49,150 We'll also need a function for resetting the house. 150 00:10:49,150 --> 00:10:53,140 So let's say a player leaves the game, the owner of the house leaves the game. 151 00:10:53,470 --> 00:10:55,960 Or the owner of the house unclaimed The house. 152 00:10:55,960 --> 00:11:01,750 We want to be able to reset the home, set the owner back to nil, reset the text, all that good stuff. 153 00:11:03,500 --> 00:11:08,090 We'll also have a function for locking the door of the house. 154 00:11:09,620 --> 00:11:14,420 And then we can also have a function for unlocking the door of the house. 155 00:11:18,250 --> 00:11:20,200 And then that's all the functions we'll need in here. 156 00:11:20,230 --> 00:11:25,630 The last thing we need to do in a module script is we need to return the table. 157 00:11:25,780 --> 00:11:33,070 So we need to return house because when a module script gets called, it has to return a table. 158 00:11:33,220 --> 00:11:40,030 And this table contains all of our functions, it contains our constructor, it contains these functions. 159 00:11:40,030 --> 00:11:47,290 And whenever I create a new house object and I call like one of these functions on it because I have 160 00:11:47,290 --> 00:11:54,010 set the index meta method to this table, it'll look and see, Hey, these functions exist within our 161 00:11:54,010 --> 00:11:56,140 house table inside of the meta table. 162 00:11:56,940 --> 00:11:58,230 And I can use them. 163 00:11:58,810 --> 00:12:00,910 So let's go ahead and fill out the rest of these. 164 00:12:00,940 --> 00:12:05,680 For reset, we just want to set the owner of the house back to nil. 165 00:12:07,310 --> 00:12:09,710 And then we also want to reset the mailbox. 166 00:12:09,710 --> 00:12:10,910 Guys text. 167 00:12:10,910 --> 00:12:13,820 So mailbox guy dot text label. 168 00:12:14,750 --> 00:12:22,220 Dot text and actually we can create a constant for it, which is why we declared this little comment 169 00:12:22,220 --> 00:12:22,860 up here. 170 00:12:22,880 --> 00:12:25,250 We'll just call it default. 171 00:12:26,090 --> 00:12:30,770 Text and it will be equal to like, owner name. 172 00:12:31,830 --> 00:12:35,160 Then we can go back down here and set this to default text. 173 00:12:35,970 --> 00:12:42,990 And when we reset the House, we also want to be able to unlock the door so we can just get access to 174 00:12:42,990 --> 00:12:47,850 the house object and call unlock door on it, just like that. 175 00:12:48,090 --> 00:12:56,790 You can kind of see how object oriented programming can be useful or perhaps you could say more logical 176 00:12:56,790 --> 00:13:02,760 in some aspects, but for most people it is a little bit confusing, so don't worry too much if you 177 00:13:02,760 --> 00:13:04,380 if you're still not really understanding. 178 00:13:04,380 --> 00:13:06,990 It's just kind of something you have to practice with over time. 179 00:13:07,860 --> 00:13:10,100 So let's go ahead and fill out these functions. 180 00:13:10,110 --> 00:13:18,780 So what I want to do when I lock the door is I want to set that is front door locked boolean to true. 181 00:13:18,810 --> 00:13:21,120 So that way we know the front door is locked. 182 00:13:21,270 --> 00:13:27,630 And then what we could do is we could loop through every single part or object that is within that front 183 00:13:27,630 --> 00:13:28,560 door model. 184 00:13:28,740 --> 00:13:36,690 So we'll do any pairs, we'll do self dot model to get the house, we'll get the front door and then 185 00:13:36,690 --> 00:13:44,640 we're going to get every single descendant of that door and then we'll check if this object. 186 00:13:46,060 --> 00:13:49,930 Is not a bass part, so is a bass part. 187 00:13:50,950 --> 00:13:55,210 Then we'll just continue looping until we find a object that is a base part. 188 00:13:55,210 --> 00:14:00,220 And when it is, we'll set object dot can collide equal to true. 189 00:14:00,580 --> 00:14:02,260 So we can't walk through the door. 190 00:14:02,990 --> 00:14:07,580 And then we can just basically copy and paste this in here and just set this to false. 191 00:14:08,670 --> 00:14:13,620 You could put this in another function since we're technically copying and pasting code here, but I'm 192 00:14:13,620 --> 00:14:16,470 just going to leave it like that because it really isn't that much code. 193 00:14:17,640 --> 00:14:21,240 So the next thing we need to do is to fill out these private functions. 194 00:14:22,030 --> 00:14:25,630 But for this one, we need to check if a player owns a home. 195 00:14:25,810 --> 00:14:31,000 And the easy way to do that is because we're saving every single house object in this table. 196 00:14:31,030 --> 00:14:33,080 We'll just loop through every single one of them. 197 00:14:33,100 --> 00:14:42,460 So for every single house object that is in the table houses, we'll check if the house object dot owner. 198 00:14:42,490 --> 00:14:42,880 Right. 199 00:14:42,880 --> 00:14:44,560 This property we made down here. 200 00:14:45,270 --> 00:14:50,730 If it's not equal to the player that gets passed to this function, then we're just going to continue. 201 00:14:51,430 --> 00:14:55,600 Otherwise, if we do find a house object that a player owns, we'll return. 202 00:14:55,600 --> 00:14:56,230 True. 203 00:14:56,530 --> 00:15:01,810 Otherwise, if we finish through this entire loop and we find no house object that the player owns, 204 00:15:01,810 --> 00:15:03,310 we'll return false. 205 00:15:03,670 --> 00:15:05,740 And we're going to be using this function in a moment. 206 00:15:06,440 --> 00:15:08,780 The next thing we want to do is fill out this function. 207 00:15:08,960 --> 00:15:11,990 So what do we want to happen when the front door is clicked? 208 00:15:12,020 --> 00:15:18,980 Well, the first thing we need to check is if the player that activated this click detector is equal 209 00:15:18,980 --> 00:15:22,910 to the self dot owner, the owner of the house object. 210 00:15:22,910 --> 00:15:25,040 If he isn't the owner. 211 00:15:25,250 --> 00:15:29,840 Then we'll just return because only the owner can unlock and lock the front door. 212 00:15:30,690 --> 00:15:35,160 Otherwise we can check if self is front door locked. 213 00:15:35,460 --> 00:15:36,090 Oops. 214 00:15:37,400 --> 00:15:40,160 If the front door is locked, then we want to do. 215 00:15:41,390 --> 00:15:42,560 Unlock a door. 216 00:15:42,590 --> 00:15:42,930 Right. 217 00:15:42,950 --> 00:15:45,950 Unlock the door inside of our house object. 218 00:15:46,370 --> 00:15:49,880 Otherwise we can lock the door. 219 00:15:50,660 --> 00:15:52,100 And that's it for that. 220 00:15:52,880 --> 00:15:56,330 Now we can listen here for when the claim part gets touched. 221 00:15:57,250 --> 00:16:04,330 So the first thing we want to check is we want to see if whatever touched our part is a player belongs 222 00:16:04,330 --> 00:16:05,100 to a player. 223 00:16:05,110 --> 00:16:12,910 So we could do we could do local player is equal to and this is why we reference the player service 224 00:16:12,910 --> 00:16:17,350 we're going to use a function called get player from character. 225 00:16:17,590 --> 00:16:22,480 We'll get the player from their character model and we'll pass other part dot parent. 226 00:16:23,120 --> 00:16:27,830 So let's say if their left foot touched the part or their leg or whatever, we get the parent of that 227 00:16:27,830 --> 00:16:30,050 leg, which should be the character model. 228 00:16:30,290 --> 00:16:34,310 And if that character model belongs to a player, we'll get the player right here. 229 00:16:35,410 --> 00:16:39,100 Otherwise, if there is no player, then we'll just return. 230 00:16:40,500 --> 00:16:44,910 But if there is a player, then we're going to use the player owns home function. 231 00:16:45,450 --> 00:16:48,810 So if this player already owns a home. 232 00:16:49,510 --> 00:16:50,290 Right. 233 00:16:50,500 --> 00:16:57,400 And we also have to check if the player isn't the owner of this home in particular that the claim part 234 00:16:57,430 --> 00:16:58,510 got touched on. 235 00:16:58,660 --> 00:17:00,430 So self dot owner. 236 00:17:01,220 --> 00:17:02,870 Then we're going to return. 237 00:17:02,960 --> 00:17:08,480 Because if a player already owns a home, we don't want them to own another one, but we also want them 238 00:17:08,480 --> 00:17:12,140 to be able to claim a home by touching that part again. 239 00:17:12,140 --> 00:17:13,730 So we have to make sure. 240 00:17:14,420 --> 00:17:17,840 That this player also doesn't own this home that they're touching. 241 00:17:18,260 --> 00:17:22,560 Next, we can check if the self touched debounce is true. 242 00:17:22,580 --> 00:17:25,400 If it is, we're going to return. 243 00:17:26,610 --> 00:17:29,380 And then we're going to use a function from the task library. 244 00:17:29,400 --> 00:17:31,190 Task dot delay. 245 00:17:31,200 --> 00:17:33,570 We're going to delay a function from running. 246 00:17:33,570 --> 00:17:36,300 We'll wait like three seconds. 247 00:17:36,660 --> 00:17:42,050 And all this function is going to do is set self dot touch debounce back to false. 248 00:17:42,060 --> 00:17:45,270 So we have a three second cooldown on that touch part. 249 00:17:45,510 --> 00:17:51,390 Now that we have all of that done, we can check if this house has a owner. 250 00:17:51,390 --> 00:17:53,100 So if self dot owner. 251 00:17:54,180 --> 00:17:59,280 And the player is the owner of the house that touched this part. 252 00:18:01,010 --> 00:18:03,530 Then that means they want to claim the house. 253 00:18:03,530 --> 00:18:07,650 So we can call that self reset function on it. 254 00:18:07,670 --> 00:18:08,570 Reset. 255 00:18:08,750 --> 00:18:09,920 We reset the house. 256 00:18:09,920 --> 00:18:12,140 There's no more owner and all is well. 257 00:18:12,560 --> 00:18:15,110 Otherwise, if the house doesn't have an owner. 258 00:18:15,290 --> 00:18:16,370 So. 259 00:18:17,070 --> 00:18:22,900 If there is no owner for the house, then we're going to self. 260 00:18:22,920 --> 00:18:28,110 We're going to call the function set owner and the owner is going to be this player. 261 00:18:29,210 --> 00:18:34,580 Otherwise, if the players already owned and another player is trying to claim it, then we're just 262 00:18:34,580 --> 00:18:35,720 not going to do anything. 263 00:18:36,700 --> 00:18:38,380 So this looks pretty good here. 264 00:18:38,380 --> 00:18:40,450 I don't think there's actually anything else we need to do. 265 00:18:40,450 --> 00:18:44,800 But as always, I'm going to explain the script one more time just to make sure you understand everything 266 00:18:44,800 --> 00:18:45,730 that's going on. 267 00:18:45,910 --> 00:18:52,390 So the purpose of this module script is to act as a blueprint for creating a new house object. 268 00:18:52,690 --> 00:18:52,960 All right. 269 00:18:52,960 --> 00:18:54,250 This gives us the blueprint. 270 00:18:54,250 --> 00:18:59,080 Everything we need to create a new house object, and we create a new house object. 271 00:18:59,080 --> 00:19:06,820 Using this constructor, it creates a new empty table with the class attached to it as a meta table. 272 00:19:07,090 --> 00:19:11,140 We set all sorts of properties in this table, such as the owner. 273 00:19:11,140 --> 00:19:17,590 What the model of the object is, you know, the click detector and all this other stuff, as well as 274 00:19:17,590 --> 00:19:19,900 attaching some functions to events. 275 00:19:20,700 --> 00:19:25,350 That way whenever we call a function on this table. 276 00:19:26,040 --> 00:19:32,220 It's going to see, Hey, there's a metal table and this metal table has an index meta method, which 277 00:19:32,220 --> 00:19:34,020 is the house class. 278 00:19:34,020 --> 00:19:39,440 And this house class contains all of the functions we need for our house. 279 00:19:39,450 --> 00:19:45,630 So if I ever wanted to set the owner for this object, if I ever wanted to open or I mean lock or unlock 280 00:19:45,660 --> 00:19:49,740 the door, then I can do it because I attached. 281 00:19:50,410 --> 00:19:51,970 This metal table. 282 00:19:52,420 --> 00:19:57,250 Now, once we have this object created, we can do whatever we want with the table. 283 00:19:57,290 --> 00:19:57,640 Right. 284 00:19:57,640 --> 00:19:59,590 We can set the owner for the table. 285 00:19:59,620 --> 00:20:07,390 We can reset the house object, set the owner to nil, reset the text on our guy, unlock the door. 286 00:20:07,390 --> 00:20:15,850 We can lock the door, and then we can listen to when parts get touched in our house to set the owner 287 00:20:15,850 --> 00:20:18,190 of the house or reset the house. 288 00:20:18,370 --> 00:20:23,230 We can listen for when the front door gets clicked by the owner to lock or unlock it and all that kind 289 00:20:23,230 --> 00:20:23,680 of good stuff. 290 00:20:23,680 --> 00:20:27,700 You can really fill this up with whatever functionality you want for the house. 291 00:20:27,910 --> 00:20:33,670 But before we can even use this module script, we need to require it in a server script. 292 00:20:33,700 --> 00:20:36,700 So let's go to server script service. 293 00:20:36,880 --> 00:20:41,590 We will create a new script and this script will be called. 294 00:20:42,610 --> 00:20:46,420 We'll just call it house class handler. 295 00:20:47,420 --> 00:20:50,540 And inside of here, we're going to need some services. 296 00:20:50,540 --> 00:20:56,690 We need the server storage service because that's where our module script is stored. 297 00:20:56,990 --> 00:20:58,490 So server storage. 298 00:20:58,490 --> 00:21:02,900 And we'll also need the player service and you'll see why in a moment. 299 00:21:05,380 --> 00:21:08,350 We're going to need a couple variables. 300 00:21:08,440 --> 00:21:11,080 First, we need to get the house class. 301 00:21:11,080 --> 00:21:13,360 So we'll call it house class. 302 00:21:13,920 --> 00:21:16,850 And we need to require the module script. 303 00:21:16,860 --> 00:21:18,630 We need to require this module script. 304 00:21:18,630 --> 00:21:25,860 So we do server storage classes, the classes folder and get the house module script. 305 00:21:26,500 --> 00:21:34,060 So because we've required the module script, all the code in here gets ran and at the very end we return 306 00:21:34,060 --> 00:21:38,170 the house table, which gets stored inside of this variable. 307 00:21:38,170 --> 00:21:39,370 And what does that mean? 308 00:21:39,370 --> 00:21:45,760 Well, that means we can access the functions inside this table like.new and we can pass our house model 309 00:21:45,790 --> 00:21:46,300 to it. 310 00:21:47,820 --> 00:21:48,210 All righty. 311 00:21:48,210 --> 00:21:48,620 Hey there. 312 00:21:48,630 --> 00:21:50,100 This is me from the Future. 313 00:21:50,100 --> 00:21:54,000 And fortunately, my software decided to not record my voice anymore. 314 00:21:54,000 --> 00:21:59,910 But I'm just going to overview what I think I was saying earlier. 315 00:22:00,060 --> 00:22:03,810 So what we're going to do here is we're going to make a simple change to this variable. 316 00:22:03,810 --> 00:22:08,880 We're actually going to make it belong to the house table itself in the module script. 317 00:22:08,880 --> 00:22:13,410 And of course you're going to have to replace that reference in several areas in the module script. 318 00:22:13,410 --> 00:22:19,740 But the reason we're doing that is so we're able to access that table from our server script, and that's 319 00:22:19,740 --> 00:22:21,810 going to be very important for us to do here. 320 00:22:21,810 --> 00:22:23,550 You'll see why in a little bit. 321 00:22:24,230 --> 00:22:27,830 Otherwise we're going to create a handler to listen to an event. 322 00:22:27,830 --> 00:22:31,490 And that event is going to be when a player gets removed from our game. 323 00:22:31,490 --> 00:22:35,750 So we'll connect a lambda function to it and get that player that left the game. 324 00:22:37,600 --> 00:22:42,130 Now, the reason we need to get the player who left the game is to check to see whether or not they 325 00:22:42,130 --> 00:22:42,820 owned a house. 326 00:22:42,820 --> 00:22:46,540 Because if they did own a house, well, we need to reset that house. 327 00:22:46,690 --> 00:22:49,510 So we're going to loop through every single table. 328 00:22:50,570 --> 00:22:52,070 That is in our house. 329 00:22:52,070 --> 00:22:53,970 Class houses, table. 330 00:22:53,990 --> 00:22:55,370 All those house objects. 331 00:22:55,370 --> 00:23:01,610 We need to loop through those and check to see if the owner is the player that left the game. 332 00:23:01,610 --> 00:23:08,030 If it isn't, we're just going to continue looping until we find a table that the player might have 333 00:23:08,030 --> 00:23:08,690 owned. 334 00:23:09,210 --> 00:23:16,440 So in this case, if the player was the owner of this house object, then we need to reset the house. 335 00:23:19,840 --> 00:23:25,300 Now, one more thing we need to do is that we have to create all of these house objects in the first 336 00:23:25,300 --> 00:23:26,140 place, right. 337 00:23:26,170 --> 00:23:29,380 We need to create all of the house objects. 338 00:23:29,380 --> 00:23:35,530 So we're going to loop through every single house model that is in that houses folder in the workspace. 339 00:23:35,530 --> 00:23:37,720 So we're going to get that houses folder. 340 00:23:37,720 --> 00:23:40,570 We're going to get all the children, all of those house models. 341 00:23:41,530 --> 00:23:46,690 And then we're going to use the constructor in our module script to create these new objects. 342 00:23:46,900 --> 00:23:53,560 So we reference our house class, pass the new function or call the new function and we pass our house 343 00:23:53,560 --> 00:23:54,190 model. 344 00:23:54,340 --> 00:24:00,580 And that creates the new house object for us which will exist in that houses table. 345 00:24:01,920 --> 00:24:08,550 Now, before we go and test our houses, there's actually an error we need to fix inside of our game 346 00:24:08,730 --> 00:24:13,590 and we need to fix a reference here within our module script. 347 00:24:14,200 --> 00:24:21,310 But now we are in our game and we can go and touch the part and it should update the name on our mailbox. 348 00:24:22,790 --> 00:24:25,520 And as you can see, we have a little problem there. 349 00:24:25,520 --> 00:24:32,090 And the reason is, is that we forgot to set that touch debounce to true within our module script. 350 00:24:32,090 --> 00:24:34,610 So we're going to fix that real quick as well. 351 00:24:36,960 --> 00:24:41,620 We also need to fix the git descendants function because I accidentally put. 352 00:24:41,640 --> 00:24:44,940 Git descendant instead of git descendants. 353 00:24:47,150 --> 00:24:52,250 But now that we have fixed those problems, if we go and touch on that brick, it's going to update 354 00:24:52,250 --> 00:24:53,900 the mailbox name to us. 355 00:24:54,050 --> 00:24:54,800 There we go. 356 00:24:54,830 --> 00:24:57,020 Udemy Louis courses home. 357 00:24:57,200 --> 00:25:01,130 And that also means we can go up and lock and unlock our door. 358 00:25:01,310 --> 00:25:02,780 So if I click, I lock it. 359 00:25:02,820 --> 00:25:05,120 If I click again, I can unlock the door. 360 00:25:05,330 --> 00:25:08,450 And I'm the only one who has the ability to do this. 361 00:25:08,780 --> 00:25:12,380 Now, if I want to claim my house, I can just touch the part again. 362 00:25:12,710 --> 00:25:18,540 And as you can see, the text on the mailbox gets reset and I won't be able to lock the door anymore. 363 00:25:18,560 --> 00:25:21,890 It will stay unlocked until someone else claims the house. 364 00:25:22,940 --> 00:25:29,030 Now because we've scripted our game in this way or our house objects in this way, we can actually duplicate 365 00:25:29,030 --> 00:25:31,700 as many of these house objects as we want. 366 00:25:32,760 --> 00:25:38,190 Because they were all belonged to separate house objects that are created from our module script. 367 00:25:39,140 --> 00:25:44,420 So back in my game, I can go ahead and claim any one of these houses, and if I go and try to claim 368 00:25:44,420 --> 00:25:48,740 another house, it won't let me claim it because I already own that other house. 369 00:25:52,480 --> 00:25:58,990 But if I unclaimed my house, I can go back, run over here and claim this new house just like that. 370 00:25:59,110 --> 00:26:01,840 And I have the ability to lock and unlock the door. 371 00:26:03,050 --> 00:26:10,310 So as you can see, object oriented programming inside of Lua, you can be quite interesting and actually 372 00:26:10,310 --> 00:26:13,940 helpful if you understand it and implement it properly. 373 00:26:13,970 --> 00:26:19,620 Hopefully this video helped you understand one way of creating a house plot system. 374 00:26:19,640 --> 00:26:22,640 Otherwise I will see you in the next lecture.